package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Shangpinfenlei;
import com.example.mapper.ShangpinfenleiMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShangpinfenleiService {

    @Resource
    ShangpinfenleiMapper shangpinfenleiMapper;

    public void add(Shangpinfenlei shangpinfenlei) {
        Account accountUser = TokenUtils.getCurrentUser();
        if ( "ADMIN".equals(accountUser.getRole())){
            shangpinfenlei.setAdminId(accountUser.getId());
        }
        shangpinfenlei.setTime(DateUtil.now());
        shangpinfenleiMapper.insert(shangpinfenlei);
    }

    public void delete(Integer id) {
        shangpinfenleiMapper.deleteById(id);
    }

    public void update(Shangpinfenlei shangpinfenlei) {
        shangpinfenleiMapper.updateById(shangpinfenlei);
    }

    public List<Shangpinfenlei> selectAll(Shangpinfenlei shangpinfenlei) {
        return shangpinfenleiMapper.selectAll(shangpinfenlei);
    }

    public PageInfo<Shangpinfenlei> selectPage(Integer pageNum, Integer pageSize, Shangpinfenlei shangpinfenlei) {
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Shangpinfenlei> list = shangpinfenleiMapper.selectAll(shangpinfenlei);
        return PageInfo.of(list);
    }



}
